package com.cyt.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.cyt.entity.LiuYan;
import com.cyt.entity.LunBoTu;
import com.cyt.util.DBManager;

public class LiuYanDaoImpl {
    
	/**
	 * 添加留言到数据库
	 * @param liuyans
	 * @return
	 */
	public int addLiuYan(LiuYan liuyans) {
		return DBManager.executeUpdate("INSERT INTO liuyan VALUES ('"+liuyans.getlYName()+"','"+liuyans.getlYDianHua()+"','"+liuyans.getlYYouXiang()+"','"+liuyans.getlYNeiRong()+"','"+liuyans.getlYId()+"','"+liuyans.getlYShiJian()+"','未读'  ,null)");
	}
	
	/**
	 * 修改留言状态 未读
	 * @param liuyan
	 * @return
	 */
	public int xiuGaiLiuYanZT(LiuYan liuyan) {
		return DBManager.executeUpdate("update liuyan set zhuangtai='已读'  where lYId="+liuyan.getlYId());
	}
	
	/**
	 * 修改留言状态 已回复  回复内容
	 * @param liuyan
	 * @return
	 */
	public int xiuGaiLiuYan(LiuYan liuyan) {
		return DBManager.executeUpdate("update liuyan set huifu='"+liuyan.getHuifu()+"', zhuangtai='已回复'    where lYId="+liuyan.getlYId());
	}
	/**
	 * 查询所有留言信息
	 * @return
	 */
	public List<LiuYan> getLiuYan() {
		List<LiuYan> list = new ArrayList<LiuYan>();
		Statement st = null;
		ResultSet rs = null;
	
		Connection conn = DBManager.getConnection();
		try {
			st = conn.createStatement();
			// 3.sql语句
			String sql = "SELECT * FROM liuyan ORDER BY lYShiJian DESC";
			// 4.执行sql语句,返回结果集
			rs = st.executeQuery(sql);
			// 5. 遍历结果集
			while (rs.next()) { 
				int lYId = rs.getInt("lYId");
				String lYName = rs.getString("lYName");
				String lYDianHua = rs.getString("lYDianHua");
				String lYYouXiang = rs.getString("lYYouXiang");
				String lYNeiRong = rs.getString("lYNeiRong");
				String lYShiJian = rs.getString("lYShiJian");
				String zhuangtai = rs.getString("zhuangtai");
				String huifu = rs.getString("huifu");

				LiuYan liuYan = new LiuYan(lYName, lYDianHua, lYYouXiang, lYNeiRong, lYId, lYShiJian, zhuangtai, huifu);// 创建用户对象
				list.add(liuYan);// 把user对象存入list中
			}
			return list;// 正常情况返回集合
         
		} catch (SQLException e) {// catch 异常执行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;// 异常返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	
	/**
	 * @param zhuangtai
	 * @return
	 */
	public List<LiuYan> getLiuYanZTTiao(String zhuangtai) {
		List<LiuYan> list = new ArrayList<LiuYan>();
		Statement st = null;
		ResultSet rs = null;
	
		Connection conn = DBManager.getConnection();
		try {
			st = conn.createStatement();
			// 3.sql语句
			String sql = "SELECT * FROM liuyan where 1=1";
			if(zhuangtai!=null && !zhuangtai.equals("")) {
				sql +=" and zhuangtai='"+zhuangtai+"'";
			}
			sql +=" ORDER BY lYShiJian DESC";
			System.out.println(sql); 
			rs = st.executeQuery(sql);
			// 5. 遍历结果集
			while (rs.next()) { 
				int lYId = rs.getInt("lYId");
				String lYName = rs.getString("lYName");
				String lYDianHua = rs.getString("lYDianHua");
				String lYYouXiang = rs.getString("lYYouXiang");
				String lYNeiRong = rs.getString("lYNeiRong");
				String lYShiJian = rs.getString("lYShiJian");
				String zhuangt = rs.getString("zhuangtai");

				LiuYan liuYan = new LiuYan(lYName, lYDianHua, lYYouXiang, lYNeiRong, lYId, lYShiJian, zhuangt);// 创建用户对象
				list.add(liuYan);// 把user对象存入list中
			}
			return list;// 正常情况返回集合
         
		} catch (SQLException e) {// catch 异常执行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;// 异常返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	/**
	 * 分页
	 * 根据状态查留言
	 * @return
	 */
	public List<LiuYan> getLiuYanZT(String zhuangtai,int page, int size) {
		List<LiuYan> list = new ArrayList<LiuYan>();
		Statement st = null;
		ResultSet rs = null;
	
		Connection conn = DBManager.getConnection();
		try {
			st = conn.createStatement();
			// 3.sql语句
			String sql = "SELECT * FROM liuyan where 1=1";
			if(zhuangtai!=null && !zhuangtai.equals("")) {
				sql +=" and zhuangtai='"+zhuangtai+"'";
			}
			sql +=" ORDER BY lYShiJian DESC LIMIT "+(page-1)*size+","+size;
			System.out.println(sql); 
			rs = st.executeQuery(sql);
			// 5. 遍历结果集
			while (rs.next()) { 
				int lYId = rs.getInt("lYId");
				String lYName = rs.getString("lYName");
				String lYDianHua = rs.getString("lYDianHua");
				String lYYouXiang = rs.getString("lYYouXiang");
				String lYNeiRong = rs.getString("lYNeiRong");
				String lYShiJian = rs.getString("lYShiJian");
				String zhuangt = rs.getString("zhuangtai");

				LiuYan liuYan = new LiuYan(lYName, lYDianHua, lYYouXiang, lYNeiRong, lYId, lYShiJian, zhuangt);// 创建用户对象
				list.add(liuYan);// 把user对象存入list中
			}
			return list;// 正常情况返回集合
         
		} catch (SQLException e) {// catch 异常执行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;// 异常返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	
	/**
	 * 根据id查 回显
	 * 
	 * @param luiYanId
	 * @return
	 */
	public LiuYan getLiuYanId(int luiYanId) {
		LiuYan liuYan=null;
		Statement st = null;
		ResultSet rs = null;
	
		Connection conn = DBManager.getConnection();
		try {
			st = conn.createStatement();
			// 3.sql语句
			String sql = "SELECT * FROM liuyan where lYId="+luiYanId;
			// 4.执行sql语句,返回结果集
			rs = st.executeQuery(sql);
			// 5. 遍历结果集
			while (rs.next()) { 
				int lYId = rs.getInt("lYId");
				String lYName = rs.getString("lYName");
				String lYDianHua = rs.getString("lYDianHua");
				String lYYouXiang = rs.getString("lYYouXiang");
				String lYNeiRong = rs.getString("lYNeiRong");
				String lYShiJian = rs.getString("lYShiJian");
				String zhuangtai = rs.getString("zhuangtai");
				String huifu = rs.getString("huifu");

				

				liuYan = new LiuYan(lYName, lYDianHua, lYYouXiang, lYNeiRong, lYId, lYShiJian, zhuangtai, huifu);// 创建用户对象
			}
			return liuYan;
         
		} catch (SQLException e) {// catch 异常执行
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;// 异常返回空
		} finally {
			DBManager.closeAll(conn, st, rs);
		}

	}
	
	/**
	 * 通过id主键删除留言
	 * @param liuYanId 前台传来的要删除的留言id
	 * @return 返回受影响行数
	 */
	public int shanLiuYan(int liuYanId) {
		return DBManager.executeUpdate("DELETE FROM liuyan WHERE lYId ="+liuYanId);
	}
	
	/**
	 * 通过id主键删除留言
	 * 批量删除
	 * @param liuYanId
	 * @return
	 */
	public int shanLiuYanSS(String liuYanIds) {
		String sql = "delete from liuyan where lYId in("+liuYanIds+")";
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
}
